/**
 * Created by YX on 2016/10/23.
 *
 */

function submitMember() {
    if($("#memberId").val()==''){
        addMember();
    }
    else {
        updateMember();
    }
}

function getMemberInfo() {
    var params = {};

    params.memberId = $("#memberId").val();
    params.memberName = $("#memberName").val();
    params.gender = $("#gender").val();
    params.birthdayStr = $("#datepicker").val();
    params.address = $("#address").val();
    params.phoneNumber = $("#phoneNumber").val();
    params.certificate = $("#certificate").val();
    params.height=$("#height").val();
    params.weight=$("#weight").val();
    params.roomNo=$("#roomNo").val();
    params.checkInDayStr=$("#checkInDay").val();

    if(params.memberName == null){
        alert("客户名称不得为空");
        return false;
    }
    if(params.certificate == null){
        alert("身份证号不得为空");
        return false;
    }

    var coon =$("#connecterInfo");
    var connectors = [];
    coon.children(".row").each(
        function (index,ele) {
            var connertor = {};
            $(this).find("input").each(

                function (index2,ele2) {
                    if($(this).attr("name") =='connId'){
                        connertor.id = $(this).val();
                    }
                    if($(this).attr("name") =='connName'){
                        connertor.name = $(this).val()
                    }
                    if($(this).attr("name") =='coonAddress'){
                        connertor.address = $(this).val();
                    }
                    if($(this).attr("name") =='connTelephone'){
                        connertor.phoneNumber = $(this).val();
                    }
                }
            );
            connectors.push(connertor);
        }
    );

    params.contacters = connectors;
    return params;

}
function addMember() {
    var params = getMemberInfo();
    $.ajax({
        type: 'POST',
        url: "/member/addMember",
        contentType:"application/json",
        data: JSON.stringify(params),
        success: function (result) {
            alert("新增客户成功");
            $("#member").click();
        },
        dataType: "text"
    });

}

function addContacter() {
    $("#connecterInfo").append(
        '<div class="row" >                                                                                      '+
        '	<div class="col-lg-6">                                                                               '+
        '		<label class="col-lg-12">家属姓名</label>                                                        '+
        '		<input type="text" name="connName" class="form-control" placeholder="点击输入...">               '+
        '	</div>                                                                                               '+
        '	<div class="col-lg-6">                                                                               '+
        '		<label class="col-lg-12">家属电话</label>                                                        '+
        '		<input type="text" name="connTelephone" class="form-control" placeholder="点击输入...">          '+
        '	</div>                                                                                               '+
        '	<div class="col-lg-12">                                                                              '+
        '		<label class="col-lg-12">家属地址</label>                                                        '+
        '		<input type="text" name="coonAddress" class="form-control" placeholder="点击输入...">            '+
        '	</div>                                                                                               '+
        '</div>                                                                                                  '
        );
}



function  editMember(_this) {

    $("#submit").removeAttr("disabled");
    $("#addContacter").removeAttr("disabled");
    $("#memberName").removeAttr("disabled");
    $("#gender").removeAttr("disabled");
    $("#datepicker").removeAttr("disabled");
    $("#certificate").removeAttr("disabled");
    $("#address").removeAttr("disabled");
    $("#phoneNumber").removeAttr("disabled");
    $("#height").removeAttr("disabled");
    $("#weight").removeAttr("disabled");
    $("#roomNo").removeAttr("disabled");
    $("#checkInDay").removeAttr("disabled");


    var coon =$("#connecterInfo");

    coon.children(".row").each(
        function (index,ele) {
            var connertor = {};
            $(this).children().each(
                function () {
                    $("input[name='connName']").removeAttr("disabled");
                    $("input[name='coonAddress']").removeAttr("disabled");
                    $("input[name='connTelephone']").removeAttr("disabled");

                }
            );
            connectors.push(connertor);
        }
    );
}

function updateMember() {
    var params = getMemberInfo();
    $.ajax({
        type: 'POST',
        url: "/member/updateMember",
        contentType:"application/json",
        data: JSON.stringify(params),
        success: function (result) {
            alert("更新客户信息");
            $("#member").click();
        },
        dataType: "text"
    });
}
function setMemberDetail(item) {

    $("#member-detail-box").empty();
    $("#member-detail-box").load("/member/memberDetail?memberId="+item);
}


